﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Xml;
using MaxLeaf.KH.DataContract;
using System.Web;
using CommonsLib.Settings;
using MaxLeaf.KH.Contract;

namespace MaxLeaf.KH.WebProxy
{
    public class Proxy : MaxLeaf.KH.Contract.IExecute
    {
        
        public IDictionary<string, object> Execute(string cmd, IDictionary<string, object> inputData)
        {
            Dictionary<string, object> outputData=new Dictionary<string,object>();
            try
            {
                MaxLeaf.KH.Contract.IExecute bll = GetBLLProxy(ref cmd);
                if (bll == null)
                {
                    return null;
                }

                 
                outputData=(Dictionary<string, object>)  bll.Execute(cmd, inputData);
                 
                return outputData;
            }
            catch
            {
                throw;
            }
             
             
             
        }

        private MaxLeaf.KH.Contract.IExecute GetBLLProxy(ref string cmd)
        {
            
            WebProxyInfo proxy = GetProxy(cmd);
            if (proxy == null) return null;
            cmd = proxy.CMD;

            Type type = Type.GetType(string.Format("{0}.{1},{0}", proxy.NameSpace, proxy.ClassName), true, true);

            return (MaxLeaf.KH.Contract.IExecute)System.Activator.CreateInstance(type);
        }

        private WebProxyInfo GetProxy(string cmd)
        {
            return MaxLeaf.KH.Settings.Configs.GetWebProxyInfo(cmd);
        }






         
    }
}
